#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"config.h"
#include<QIcon>
#include<QPainter>
#include<QMouseEvent>
#include<QPoint>
#include"bullet.h"
#include<ctime>
#include<QPlainTextEdit>
#include<QtMultimedia>
#include <QSoundEffect>
#include <QMediaPlayer>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    initscene();

    //启动
    playgame();


}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::initscene()
{

    isbegin=false;
    isselect=false;
    ui->title->setText("钢铁防线");
    ui->menu->setText("按“A”选择普通飞机  按“S”选择超级飞机");
    ui->introduction->setText("勇士！ 保卫你的防线吧！");
    beginbackground.load(":/picture/background1.jpg");
    selectbackground.load(":/picture/background2.jpg");
    //尺寸
    setFixedSize(Game_WIDTH,Game_HEIGHT);
    //标题
    setWindowTitle(Game_title);
    //图标
    setWindowIcon(QIcon(":/picture/plane3"));
    //定时器
    m_Timer.setInterval(Game_rate);
    //敌机间隔
    m_recorder=0;

    //随机数种子
    srand((unsigned int)time(NULL));
    pointSound->setSource(QUrl::fromLocalFile(":/music/point.wav"));
    startSound->setSource(QUrl::fromLocalFile(":/music/background.wav"));
    startSound->setLoopCount(QSoundEffect::Infinite);
    elseSound->setSource(QUrl::fromLocalFile(":/music/else.wav"));
    elseSound->setLoopCount(QSoundEffect::Infinite);
}

void MainWindow::playgame()
{

    //启动定时器
    //m_Timer.start();

//
    elseSound->play();

     pointSound->play();
    //信号
    connect(&m_Timer,&QTimer::timeout,[=](){                         

        //升级判断
        levelstatu();

        //子弹
        shoot();

        //技能
        sumskill();

        //敌机
        enemyToscene();
        bossstatu();

        //更新坐标
        updateposition();

        //检测碰撞
        collisionDetection();

         //绘制到屏幕中

          update();

        //
        scoretoscene();
        //
        difficultstatutoscene();
        //
        hptoscene();
        //
        //
        gradetoscene();

        //结束
         ending();

         recordtoscene();
     });

}

void MainWindow::updateposition()
{
    //更新地图坐标
    m_map.mapPosition();
    boss.bossupdateposition();

    //计算非空闲子弹坐标
    bulletposition();
    //非空闲敌机坐标
    for(int i=0;i<enemy_num;i++)
    {
        if(m_enemy[i].m_free==false)
        {
            updatenemyspeed();

            m_enemy[i].updateposition();

        }
    }

    //播放的图片
    for(int i=0;i<bomb_num;i++)
    {
        if(m_boom[i].m_free==false)
        {
            m_boom[i].updateinfo();
        }
    }

    for(int i=0;i<10;i++)
    {
        if(m_skill2[i].free==false)
        {
            m_skill2[i].updatepositions2();
        }
    }
    /*temp.m_free=false;
    temp.updatepositon();*/
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    //地图
    if(isbegin&&isselect)
    {
        painter.drawPixmap(m_map.m_map1_x,m_map.m_map1_y,m_map.m_map1);
        painter.drawPixmap(m_map.m_map2_x,m_map.m_map2_y,m_map.m_map2);
        //子弹、飞机
        if(m_plane.whichplane)
        {
            for(int i=0;i<bullet_num;i++)
            {
                if(m_plane.m_bullet[i].m_free==false)
                {
                    painter.drawPixmap(m_plane.m_bullet[i].m_x,m_plane.m_bullet[i].m_y,m_plane.m_bullet[i].m_Bullet1);
                }
            }
            painter.drawPixmap(m_plane.m_x,m_plane.m_y,m_plane.m_plane);
        }
        else
        {
            for(int i=0;i<bullet_num;i++)
            {
                if(m_plane.m_bullet[i].m_free==false)
                {
                    painter.drawPixmap(m_plane.m_bullet[i].m_x,m_plane.m_bullet[i].m_y,m_plane.m_bullet[i].m_bullet2);
                }
            }
            painter.drawPixmap(m_plane.m_x,m_plane.m_y,m_plane.m_plane2);
        }
        if(!boss.m_free_boss)
        {
            for(int i=0;i<20;i++)
            {
                if(!boss.bossbullet[i].m_free_boss)
                {
                    painter.drawPixmap(boss.bossbullet[i].m_x_boss,boss.bossbullet[i].m_y_boss,boss.bossbullet[i].m_Bulletboss);
                }

            }

            painter.drawPixmap(boss.m_x_boss,boss.m_y_boss,boss.boss);
        }
        //绘制敌机
        for(int i=0;i<enemy_num;i++)
        {
            if(m_enemy[i].m_free==false)
            {
                painter.drawPixmap(m_enemy[i].m_x,m_enemy[i].m_y,m_enemy[i].m_enemy);
            }
        }

        //绘制爆炸
        for(int i=0;i<bomb_num;i++)
        {
            if(m_boom[i].m_free==false)
            {
                painter.drawPixmap(m_boom[i].m_x,m_boom[i].m_y,m_boom[i].m_pixArr[m_boom[i].m_index]);
            }
        }

        for(int i=0;i<10;i++)
        {
            if(m_skill2[i].free==false)
            {
                painter.drawPixmap(m_skill2[i].m_x,m_skill2[i].m_y,m_skill2[i].daodan);
            }
        }
    }
    if(!isbegin&&!isselect)
    {
        painter.drawPixmap(0,0,beginbackground);
    }
    if(!isbegin&&isselect)
    {
        painter.drawPixmap(0,0,selectbackground);
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    setMouseTracking(true);
    int x = event->x() - m_plane.m_Rect.width()*0.5;
    int y = event->y() - m_plane.m_Rect.height()*0.5;
    //边界
    if(x<=0)
    {
        x=0;
    }
    if(x>=Game_WIDTH-m_plane.m_Rect.width())
    {
        x=Game_WIDTH-m_plane.m_Rect.width();
    }
    if(y<=0)
    {
        y=0;
    }
    if(y>=Game_HEIGHT-m_plane.m_Rect.height())
    {
        y=Game_HEIGHT-m_plane.m_Rect.height();
    }
    m_plane.setposition(x,y);
}

void MainWindow::enemyToscene()
{
    m_recorder++;
    if(m_recorder<enemy_interval)
    {
        return;
    }
    else
    {
        if(!difficultup)
        {
            if(m_plane.level==3)
            {
                m_recorder=14;
            }
            if(m_plane.level==2)
            {
                m_recorder=11;
            }
            if(m_plane.level==1)
            {
                m_recorder=0;
            }
        }
        else
        {
            if(m_plane.level==1)
            {
                m_recorder=-20;
            }
            if(m_plane.level==2)
            {
                m_recorder=1;
            }
            if(m_plane.level==3)
            {
                m_recorder=6;
            }
        }
        for(int i=0;i<enemy_num;i++)
        {

            if(m_enemy[i].m_free)
            {
                m_enemy[i].m_free=false;
                    //坐标
                m_enemy[i].m_x=rand()%(Game_WIDTH-m_enemy[i].m_rect.width());
                m_enemy[i].m_y=-m_enemy[i].m_rect.height();
                break;
            }
        }
    }
}



void MainWindow::bossstatu()
{
    if(difficultup)
    {
        if(m_score.Score>=200)
        {
            if((m_score.Score%70==0||m_score.Score%71==0)&&boss.m_free_boss==true)
            {
                boss.m_free_boss=false;
                boss.m_x_boss=rand()%(Game_WIDTH-boss.m_rect_boss.width());
                boss.m_y_boss=rand()%(150);
            }
        }
    }
    else
    {
        if(m_score.Score>=200)
        {
            if(m_score.Score%80==0&&boss.m_free_boss==true)
            {
                boss.m_free_boss=false;
                boss.m_x_boss=rand()%(Game_WIDTH-boss.m_rect_boss.width());
                boss.m_y_boss=rand()%(150);
            }
        }
    }
    if(!boss.m_free_boss)
    {

        ui->boss->setText("BOSS APPEARED!   HP: "+QString::number(boss.boss_hp));
        if(boss.boss_hp<=0)
        {
            boss.m_free_boss=true;
            boss.boss_hp=10;
            ui->boss->clear();
        }
    }
}

void MainWindow::collisionDetection()
{
    //遍历所有非空闲飞机
    for(int i=0;i<enemy_num;i++)
    {
        //判断是否已经爆炸
        bool collision=false;
        //空闲飞机则继续循环
        if(m_enemy[i].m_free==true)
        {
            continue;
        }
        else
        {
            for(int j=0;j<bullet_num;j++)
            {
                //空闲子弹也继续循环
                if(m_plane.m_bullet[j].m_free==true)
                {
                    continue;
                }
                else
                {
                    //飞机和子弹是否相交
                    if(m_enemy[i].m_rect.intersects(m_plane.m_bullet[j].m_rect))
                    {
                        m_enemy[i].hp--;
                        m_plane.m_bullet[j].m_free=true;
                        if(m_enemy[i].hp==0)
                        {
                            m_enemy[i].hp=hpnow;
                            scoreupdate();
                            collision=true;
                            m_enemy[i].m_free=true;
                            grade++;
//播放爆炸效果
                            for(int k=0;k<15;k++)
                            {
                                if(m_boom[k].m_free)//空闲
                                {
                                    m_boom[k].m_free=false;
                                    //更新坐标
                                    m_boom[k].m_x=m_enemy[i].m_x;
                                    m_boom[k].m_y=m_enemy[i].m_y;
                                    break;
                                }
                            }
                        }
                    }
                }
                for(int j=0;j<skill2_num;j++)
                {
                    //空闲导弹也继续循环
                    if(m_skill2[j].free==true)
                    {
                        continue;
                    }
                    //技能是否相撞
                    else
                    {
                        if(m_enemy[i].m_rect.intersects(m_skill2[j].m_rect))
                        {
                            m_enemy[i].m_free=true;
                            m_skill2[j].free=true;

                            //播放爆炸效果
                            if(collision==false)
                            {
                                scoreupdate();
                                grade++;
                                for(int k=15;k<25;k++)
                                {
                                    if(m_boom[k].m_free)//空闲
                                    {
                                        m_boom[k].m_free=false;
                                        //更新坐标
                                        m_boom[k].m_x=m_enemy[i].m_x;
                                        m_boom[k].m_y=m_enemy[i].m_y;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if(!boss.m_free_boss)
    {
        for(int i=0;i<bullet_num;i++)
        {
            if(m_plane.m_bullet[i].m_free)
            {
                continue;
            }
            else
            {
                if(boss.m_rect_boss.intersects(m_plane.m_bullet[i].m_rect))
                {
                    m_plane.m_bullet[i].m_free=true;
                    boss.boss_hp--;
                    for(int k=25;k<bomb_num;k++)
                    {
                        if(m_boom[k].m_free)//空闲
                        {
                            m_boom[k].m_free=false;
                            //更新坐标
                            m_boom[k].m_x=boss.m_x_boss+boss.m_rect_boss.width()*0.5;
                            m_boom[k].m_y=boss.m_y_boss+boss.m_rect_boss.height()*0.5;
                            break;
                        }
                    }
                }
            }
        }
        for(int i=0;i<20;i++)
        {
            if(boss.bossbullet[i].m_free_boss)
            {
                continue;
            }
            else
            {
                if(boss.bossbullet[i].m_rect_boss.intersects(m_plane.m_Rect))

                boss.bossbullet[i].m_free_boss=true;
            }
        }
    }
}

void MainWindow::sumskill()
{
    if(m_plane.whichplane)
    {
        //第一个技能判断
        skill1statu();

        //第二个技能
        skill2statu();
        skill2toscene();

        //第三个技能
        skill3statu();

        //第四个技能
        skill4statu();
        skillstatutoscene();
    }
}

void MainWindow::skill1statu()
{
    if(!s1)
    {
        if(m_plane.level==1)
        {
            if(m_score.Score%2==0)
            {
                skillrecorder1++;
            }
            skillrecorder1++;
            if(skillrecorder1%15==0)
            {
                m_skill1.updatestatu1();
                skillrecorder1=0;
            }
        }
        else
        {
            if(m_score.Score%1==0)
            {
                skillrecorder1++;
            }
            skillrecorder1++;
            if(skillrecorder1%15==0)
            {
                m_skill1.updatestatu1();
                skillrecorder1=0;
            }
        }

    }
    else
    {
        m_skill1.lasttime1--;
        if(m_skill1.lasttime1==0)
        {
            s1=false;
            for(int i=0;i<bullet_num;i++)
            {
                m_plane.m_bullet[i].Bullet_interval=bullet_interval;

            }
            m_skill1.lasttime1=400;
        }
    }
}

void MainWindow::skill1begin()
{
    if(m_skill1.skill1)
    {
        m_skill1.load1=0;
        m_skill1.skill1=false;
        for(int i=0;i<bullet_num;i++)
        {
            m_plane.m_bullet[i].Bullet_interval-=15;

        }
        s1=true;
    }
}

void MainWindow::skill2toscene()
{
    if(s2)
    {
        recorder++;
        if(recorder<30)
        {
            return;
        }
        else
        {
            recorder=0;
            for(int i=0;i<skill2_num;i++)
            {
                if(m_skill2[i].free)
                {
                    m_skill2[i].free=false;

                    //坐标
                    m_skill2[i].m_x=rand()%(Game_WIDTH-m_enemy[i].m_rect.width());
                    m_skill2[i].m_y=Game_HEIGHT-m_skill2[i].m_rect.height();
                    break;
                }
            }
        }
    }
}

void MainWindow::skill2statu()
{

        if(!s2)
        {
            if(m_plane.level==1)
            {
                if(m_score.Score%3==0)
                {
                    skillrecorder2++;
                }
                skillrecorder2++;
                if(skillrecorder2%20==0)
                {
                    m_skill2[0].updatestatu2();
                    skillrecorder2=0;
                }
            }
            else
            {
                if(m_score.Score%2==0)
                {
                    skillrecorder2++;
                }
                skillrecorder2++;
                if(skillrecorder2%15==0)
                {
                    m_skill2[0].updatestatu2();
                    skillrecorder2=0;
                }
            }
        }
        else
        {
            m_skill2[0].lasttime2--;
            if(m_skill2[0].lasttime2==0)
            {
                s2=false;
                m_skill2[0].lasttime2=400;
            }
        }


}

void MainWindow::skill2begin()
{
    if(m_skill2[0].skill2)
    {
        s2=true;
        m_skill2[0].load2=0;
        m_skill2[0].skill2=false;
    }

}

void MainWindow::skill3statu()
{

      skillrecorder3++;
        if(m_plane.level==1)
        {
            if(m_score.Score%4==0)
            {
                skillrecorder3++;
            }
            if(skillrecorder3%40==0)
            {
                m_skill1.updatestatu3();
                skillrecorder3=0;
            }
        }
        else
        {
            if(m_score.Score%3==0)
            {
                skillrecorder3++;
            }
            if(skillrecorder3%30==0)
            {
                m_skill1.updatestatu3();
                skillrecorder3=0;
            }
        }



}

void MainWindow::skill3begin()
{
    if(m_skill1.skill3)
    {
        for(int i=0;i<enemy_num;i++)
        {
            if(!m_enemy[i].m_free)
            {
                m_enemy[i].m_free=true;
                m_score.increase1();

            }
        }

        m_skill1.load3=0;
        m_skill1.skill3=false;
    }
}

void MainWindow::skill4statu()
{

        skillrecorder4++;
        if(m_plane.level==1)
        {
            if(m_score.Score%4==0)
            {
                skillrecorder4++;
            }
            if(skillrecorder4%40==0)
            {
                m_skill1.updatestatu4();
                skillrecorder4=0;
            }
        }
        else
        {
            if(m_score.Score%4==0)
            {
                skillrecorder4++;
            }
            if(skillrecorder4%30==0)
            {
                m_skill1.updatestatu4();
                skillrecorder4=0;
            }
        }

}

void MainWindow::skill4begin()
{
    if(m_skill1.skill4)
    {
        player_hp+=10;
        m_skill1.load4=0;
        m_skill1.skill4=false;
    }
}

void MainWindow::maptremble()
{

    if(m_map.m_map1_x<-4)
    {
        m_map.m_map1_x+=4;
        m_map.m_map2_x+=4;
    }
    else
    {
        m_map.m_map1_x-=4;
        m_map.m_map2_x-=4;
    }

}

void MainWindow::levelup()
{
    if(level&&m_plane.level<3)
    {
        m_plane.level++;
        level=false;
        grade=0;
         pointSound->play();
        if(difficultup)
        {
            for(int i=0;i<bullet_num;i++)
            {
                 m_plane.m_bullet[i].m_speed+=2;
            }
        }
    }
}

void MainWindow::levelstatu()
{
    if(grade>=100)
    {
        grade=100;
        level=true;

    }
}

void MainWindow::gradetoscene()
{
    if(m_plane.level==3)
    {
        ui->grade->setText("LV:"+QString::number(m_plane.level)+" (max)");
    }
    else
    {
        ui->grade->setText("LV:"+QString::number(m_plane.level));
    }
    if(grade<100)
    {
        ui->pace->setText(QString::number(grade)+"%");
    }
    else
    {
        ui->pace->setText("upalready");
    }
}

void MainWindow::shoot()
{
    if(m_plane.level==1)
    {
        m_plane.shoot1();
    }
    if(m_plane.level==2)
    {
        m_plane.shoot2();
    }
    if(m_plane.level==3)
    {
        m_plane.shoot3();
        m_plane.shoot4();
    }
    if(!boss.m_free_boss)
    {
        boss.bossshot();
    }
    if(boss.m_free_boss)
    {
        for(int i=0;i<20;i++)
        {
            boss.bossbullet[i].m_free_boss=true;
        }
    }
}

void MainWindow::scoreupdate()
{
    if(!difficultup)
    {
        m_score.increase1();
    }
    else
    {
        m_score.increase2();

    }
}

void MainWindow::scoretoscene()
{
    ui->score->setText("SCORE:"+QString::number(m_score.Score));

}

void MainWindow::judgmenttoscene()
{

    switch (m_score.Score/100) {
    case 0:
        ui->judgment->setText("SO BAD!");
        break;
    case 1:
        ui->judgment->setText("SO BAD!");
        break;
    case 2:
        ui->judgment->setText("Ordinary");
        break;   
    case 3:
        ui->judgment->setText("Acceptable");
        break;   
    case 4:
        ui->judgment->setText("Good");
        break;
    case 5:
        ui->judgment->setText("Excellent");
        break;
    default:
        ui->judgment->setText("GOOOOOD!");
        break;
    }
}

void MainWindow::hurt()
{
    for(int i=0;i<enemy_num;i++)
    {
        if(!m_enemy[i].m_free)
        {
            if(m_enemy[i].m_y>=Game_HEIGHT-m_enemy[i].m_rect.height())
            {
                player_hp--;

                maptremble();
                m_enemy[i].m_free=true;
            }
        }
    }
    for(int i=0;i<20;i++)
    {
        if(!boss.bossbullet[i].m_free_boss)
        {
            if(boss.bossbullet[i].m_y_boss>=Game_HEIGHT)
            {
                player_hp--;
                boss.bossbullet[i].m_free_boss=true;
            }

        }

    }
}

void MainWindow::hptoscene()
{
    hurt();
    ui->labelhp->setText("HP:"+QString::number(player_hp));
}

void MainWindow::skillstatutoscene()
{

        if(m_skill1.load1<100)
        {
            ui->skill1->setText("超级火力:"+QString::number(m_skill1.load1)+"%");
        }
        else
        {
            ui->skill1->setText("超级火力:按1释放");
        }
        if(m_skill2[0].load2<100)
        {
            ui->skill2->setText("火力支援:"+QString::number(m_skill2[0].load2)+"%");
        }
        else
        {
            ui->skill2->setText("火力支援:按2释放");
        }
        if(m_skill1.load3<100)
        {
            ui->skill3->setText("斩杀:"+QString::number(m_skill1.load3)+"%");
        }
        else
        {
            ui->skill3->setText("斩杀:按3释放");
        }
        if(m_skill1.load4<100)
        {
            ui->skill4->setText("治疗:"+QString::number(m_skill1.load4)+"%");
        }
        else
        {
            ui->skill4->setText("治疗:按4释放");
        }

}

void MainWindow::bulletposition()
{
    if(m_plane.level<=2)
    {
        for(int i=0;i<bullet_num;i++)
        {
            if(m_plane.m_bullet[i].m_free==false)
            {
                m_plane.m_bullet[i].updateposition1();
            }
        }
    }
    else
    {
        for(int i=0;i<bullet_num/3;i++)
        {
            if(m_plane.m_bullet[i].m_free==false)
            {
                m_plane.m_bullet[i].updateposition2();
            }
        }
        for(int i=bullet_num/3;i<(bullet_num/3)*2;i++)
        {
            if(m_plane.m_bullet[i].m_free==false)
            {
                m_plane.m_bullet[i].updateposition1();
            }
        }
        for(int i=(bullet_num/3)*2;i<bullet_num;i++)
        {
            if(m_plane.m_bullet[i].m_free==false)
            {
                m_plane.m_bullet[i].updateposition3();
            }
        }

    }
    if(!boss.m_free_boss)
    {
        for(int i=0;i<20;i++)
        {
            if(!boss.bossbullet[i].m_free_boss)
            {
                boss.bossbullet[i].updatepositionboss();
            }
        }
    }
}

void MainWindow::ending()
{
    if(player_hp==0)
    {
    m_Timer.stop();
    ui->final_2->setText("YOUR SCORE: "+QString::number(m_score.Score));
    judgmenttoscene();
    if(newscore)
    {
        ui->congratulate->setText("NEW RECORD: "+QString::number(m_score.Score));
    }
    }


}

void MainWindow::regame()
{
    for(int i=0;i<bullet_num;i++)
    {
        m_plane.m_bullet[i].m_free=true;
    }
    for(int i=0;i<enemy_num;i++)
    {
        m_enemy[i].m_free=true;
    }
    m_plane.m_x=(Game_WIDTH-m_plane.m_plane.width())/2;
    m_plane.m_y=Game_HEIGHT-m_plane.m_plane.height();
    for(int i=0;i<bullet_num;i++)
    {
        m_plane.m_bullet[i].m_speed=5;
    }
    m_score.Score=0;
    player_hp=100;
    m_skill1.load1=0;
    m_skill1.load3=0;
    m_skill2[0].load2=0;
    m_plane.level=1;
    m_Timer.start();
    grade=0;
    ui->final_2->clear();
    ui->judgment->clear();
    ui->congratulate->clear();
    newscore=false;
    m_skill1.load4=0;
    if(difficultup)
    {
        player_hp=50;

    }
    else
    {
        player_hp=40;
    }
    boss.m_free_boss=true;
    ui->boss->clear();
}

void MainWindow::recordtoscene()
{
    if(record<m_score.Score)
    {
        record=m_score.Score;
        newscore=true;
    }
    ui->record->setText("RECORD: "+QString::number(record));

}



void MainWindow::difficult()
{
    hpnow=2;
    for(int i=0;i<enemy_num;i++)
    {
        m_enemy[i].hp=hpnow;
    }
    difficultup=true;
    elseSound->stop();
    startSound->play();
}

void MainWindow::simple()
{
    hpnow=1;
    for(int i=0;i<enemy_num;i++)
    {
        m_enemy[i].hp=hpnow;
    }
    difficultup=false;
    elseSound->stop();
    startSound->play();
}

void MainWindow::updatenemyspeed()
{
    if(!difficultup)
    {
        for(int i=0;i<enemy_num;i++)
        {
            if(m_plane.level==1)
            {
                m_enemy[i].m_speed=4;
            }
            if(m_plane.level==2)
            {
                m_enemy[i].m_speed=6;
            }
            if(m_plane.level==3)
            {
                m_enemy[i].m_speed=8;
            }
        }
    }
    else
    {
        for(int i=0;i<enemy_num;i++)
        {
            if(m_plane.level==1)
            {
                m_enemy[i].m_speed=2;
            }
            if(m_plane.level==2)
            {
                m_enemy[i].m_speed=4;
            }
            if(m_plane.level==3)
            {
                m_enemy[i].m_speed=5;
            }
        }
    }
}

void MainWindow::difficultstatutoscene()
{

    if(!difficultup)
    {
        ui->difficultstatu->setText("当前：简单");
    }
    else
    {
        ui->difficultstatu->setText("当前：困难");
    }
}

void MainWindow::keyPressEvent(QKeyEvent *event)  {
    switch (event->key()) {
    case Qt::Key_1:
        skill1begin();
         pointSound->play();
        break;
    case Qt::Key_2:
        skill2begin();
        pointSound->play();
        break;
    case Qt::Key_3:
        skill3begin();
        break;
    case Qt::Key_4:
        skill4begin();
        break;
    case Qt::Key_A:
        isselect=true;
        update();
        ui->title->clear();
        ui->menu->setText("请选择你的难度：");
        ui->introduction->clear();
        break;
    case Qt::Key_S:
        isselect=true;
        m_plane.whichplane=true;
        update();
        ui->title->clear();
        ui->menu->setText("请选择你的难度：");
        ui->introduction->clear();
        break;
    default:
        break;
    }
}


void MainWindow::on_pushButton_clicked()
{
    levelup();
    pointSound->play();
}



void MainWindow::on_pause_clicked()
{
    if(m_Timer.isActive())
    {
        m_Timer.stop();
    }
    startSound->stop();
    elseSound->play();
}



void MainWindow::on_continue_2_clicked()
{
    m_Timer.start();
    elseSound->stop();
    startSound->play();
}



void MainWindow::on_pushButton_2_clicked()
{
    regame();
}



void MainWindow::on_difficult_clicked()
{
    if(isselect)
    {
        pointSound->play();
        difficult();
        isbegin=true;
        ui->title->clear();
        ui->menu->clear();
        ui->introduction->clear();
        regame();
    }
}



void MainWindow::on_simple_clicked()
{
    if(isselect)
    {
        pointSound->play();
        isbegin=true;
        ui->title->clear();
        ui->menu->clear();
        ui->introduction->clear();
        simple();
        regame();
    }
}


void MainWindow::on_back_clicked()
{
    if(isbegin)
    {
        isbegin=false;
        isselect=false;
        m_Timer.stop();
        m_plane.whichplane=false;
    }
    else
    {
        isselect=false;
        m_plane.whichplane=false;
    }
    ui->title->setText("钢铁防线");
    ui->menu->setText("按“A”选择普通飞机  按“S”选择超级飞机");
    ui->introduction->setText("勇士！ 保卫你的防线吧！");
    ui->score->clear();
    ui->skill1->clear();
    ui->skill2->clear();
    ui->skill3->clear();
    ui->skill4->clear();
    ui->judgment->clear();
    ui->final_2->clear();
    ui->grade->clear();
    ui->pace->clear();
    ui->labelhp->clear();
    ui->difficultstatu->clear();
    ui->congratulate->clear();
    update();
    startSound->stop();
    elseSound->play();
}

